﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyThuoc.BUS;
namespace QuanLyThuoc.PresentionLayer
{
    public partial class frmTonKho : Form
    {
        public int keyValue = 0;
        int vitri = 0;
        bool plAddMode = false;
        bool plEditMode = false;
        BindingSource dsTonkho;
        private AutoCompleteStringCollection auBN = new AutoCompleteStringCollection();
        private AutoCompleteStringCollection auTenThuoc = new AutoCompleteStringCollection();

        public frmTonKho()
        {
            InitializeComponent();
        }

       

        private void frmTonKho_Load(object sender, EventArgs e)
        {
            this.HienThiDanhSach();
        }
        private void HienThiDanhSach()
        {
            dsTonkho = new BindingSource();
            dsTonkho.DataSource = TonKhoBUS.SelectAll_TonKho();
            grdList.DataSource = dsTonkho;

        }
        private void ResetControl()
        {
            this.txtHanSD.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDToa.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDTon.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDThuoc.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtNgayNhap.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtNgaySX.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtNgayXuat.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSL_N.ReadOnly = !(this.plAddMode || this.plEditMode);

            this.txtSL_X.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSoPhieu.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtTenThuoc.ReadOnly = !(this.plAddMode || this.plEditMode);
            //this.txt.ReadOnly = !(this.plAddMode || this.plEditMode);

            this.cmdAdd.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.save24 : QuanLyThuoc.Properties.Resources.Add24;
            this.cmdEdit.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.undo24 : QuanLyThuoc.Properties.Resources.Edit24;

            this.cmdAdd.Text = (this.plAddMode || this.plEditMode) ? "Lưu [F2]" : "Thêm [F2]";
            this.cmdEdit.Text = (this.plAddMode || this.plEditMode) ? "Hủy [F4]" : "Sửa [F4]";

            this.grdList.Enabled = !this.plAddMode;

        }
        private void LoadTenThuocTheoID()
        {
            this.auTenThuoc.Clear();
            DataTable dtthuoc = ThuocBUS.LayTenThuocTheoID();
            string lcValue = "";
            foreach (DataRow row in dtthuoc.Rows)
            {

                lcValue = row["TenThuoc"].ToString().PadRight(100, ' ') + "|" + row["IDThuoc"].ToString().Trim();
                //this.auTenNCC.Add(row["TenNCC"].ToString());
                this.auTenThuoc.Add(lcValue);
            }
            this.txtTenThuoc.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtTenThuoc.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtTenThuoc.AutoCompleteCustomSource = this.auTenThuoc;
        }
        private void GanDK(DataGridViewRow r)
        {
            try
            {
                if (grdList.Rows.Count != 0)
                {
                    this.txtIDTon.Text = r.Cells[0].Value.ToString();

                    this.txtSoPhieu.Text = r.Cells[1].Value.ToString();
                    this.txtIDThuoc.Text = r.Cells[2].Value.ToString();
                    this.txtTenThuoc.Text = r.Cells[3].Value.ToString();
                    this.txtIDToa.Text = r.Cells[4].Value.ToString();
                    this.txtNgayNhap.Text = r.Cells[5].Value.ToString();
                    this.txtSL_N.Text = r.Cells[6].Value.ToString();
                    this.txtNgayXuat.Text = r.Cells[7].Value.ToString();
                    this.txtSL_X.Text = r.Cells[8].Value.ToString();
                    this.txtNgaySX.Text = r.Cells[9].Value.ToString();

                    this.txtHanSD.Text = r.Cells[10].Value.ToString();
                    
                }
                else
                {
                    this.txtIDTon.Text="";
                    this.txtSoPhieu.Text = "";
                    this.txtIDThuoc.Text = "";
                    this.txtTenThuoc.Text = "";
                    this.txtIDToa.Text = "";
                    this.txtNgayNhap.Text = "";
                    this.txtSL_N.Text = "";
                    this.txtNgayXuat.Text = "";
                    this.txtSL_X.Text = "";
                    this.txtNgaySX.Text = "";

                    this.txtHanSD.Text = "";
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void AddBlank()
        {
            this.txtSoPhieu.Text = "";
            this.txtIDThuoc.Text = "";
            this.txtTenThuoc.Text = "";
            this.txtIDToa.Text = "";
            this.txtNgayNhap.Text = "";
            this.txtSL_N.Text = "";
            this.txtNgayXuat.Text = "";
            this.txtSL_X.Text = "";
            this.txtNgaySX.Text = "";

            this.txtHanSD.Text = "";
        }
        private void AddNew()
        {
            this.plAddMode = true;
            this.plEditMode = false;


        }
        private void Edit()
        {
            this.plEditMode = true;
            this.plAddMode = false;
        }
        private void Undo()
        {

            vitri = grdList.CurrentCell.RowIndex;
            this.plAddMode = false;
            this.plEditMode = false;
        }
        private void Save()
        {


           
            string sophieu = this.txtSoPhieu.Text;
            int mathuoc = int.Parse(this.txtIDThuoc.Text);
            string tenthuoc = this.txtTenThuoc.Text;
            string idtoa = this.txtIDToa.Text;
            DateTime ngaynhap =Convert.ToDateTime(this.txtNgayNhap.Text);
            int slnhap =int.Parse(this.txtSL_N.Text);
            DateTime ngayxuat = Convert.ToDateTime(txtNgayUong.Text);
            int slXuat = int.Parse(txtSL_X.Text);
            DateTime ngaySX = Convert.ToDateTime(txtNgaySX.Text);
            DateTime hansd = Convert.ToDateTime(txtHanSD.Text);

            //DataTable dt = new DataTable();
            //dt = BenhNhanBUS.SelectID_BenhNhanMAX();
            //if (txtIDBN.Text == "")
            //{
            //    IDBN = Convert.ToInt32(dt.Rows[0]["IDBN"].ToString()) + 1;
            //}
            //else
            //{
            //    IDBN = int.Parse(txtIDBN.Text);
            //}


            if (string.IsNullOrEmpty(this.txtIDTon.Text.Trim()))
            {

                //string MaToa = Convert.ToDateTime(DateTime.Today).ToString("dd/MM/yyyy") + "-" + IDBN;
                //if (KiemTraNhap())
                //{
                TonKhoBUS.InsertTonKho(sophieu, ngaynhap, ngaySX, hansd, slnhap, matoa, mathuoc, ngayxuat, slXuat);
                //}


                //if (grdList.RowCount > 0)
                //{
                //    DataGridViewRow r = this.grdList.SelectedRows[0];
                //    this.AddBlank();

                //    Gan_Click();
                //}
            }
            else
            {
                int MaTon = int.Parse(txtIDTon.Text);
                if (KiemTraNhap())
                {
                    ToaThuocBUS.UpdateToaThuoc(MaToa, MaThuoc, IDBN, TenBN, NamSinh, GioiTinh, DiaChi, SoDT, ChuanDoan, NgayUong, NgayTk);
                }
            }

        }

        private void Delete()
        {
            try
            {
                int maton =int.Parse(txtIDTon.Text);

                if (MessageBox.Show("Bạn có muốn xóa   mã tồn '" + txtIDTon.Text + "'không ??", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (vitri == grdList.RowCount)
                    {
                        TonKhoBUS.Delete_TonKho(maton);
                        this.AddBlank();
                        grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                        grdList.Rows[vitri - 1].Selected = true;
                        Gan_Click();
                    }
                    if (vitri == 0)
                    {
                        TonKhoBUS.Delete_TonKho(maton);
                        Gan_Click();
                        grdList.CurrentCell = grdList.Rows[vitri + 1].Cells[0];
                        grdList.Rows[vitri + 1].Selected = true;
                        this.AddBlank();
                        Gan_Click();
                    }
                    if (vitri > 0 && vitri < grdList.RowCount)
                    {
                        TonKhoBUS.Delete_TonKho(maton);
                        this.AddBlank();
                        grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                        grdList.Rows[vitri - 1].Selected = true;
                        Gan_Click();
                    }

                }
            }
            catch (Exception ex)
            {
                // this.AddBlank();
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData.Equals(Keys.Escape))
            {
                this.keyValue = 0;
                if (!(this.plAddMode || this.plEditMode))
                {
                    this.Close();
                }
                else
                {
                    DialogResult dr = MessageBox.Show("Bạn có muốn đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                        this.Close();
                }
            }
            else if (keyData.Equals(Keys.F2))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Save();
                else
                    this.AddNew();

                this.ResetControl();
            }
            else if (keyData.Equals(Keys.F3))
            {
                if (!(this.plAddMode || this.plEditMode))
                {
                    if (string.IsNullOrEmpty(this.txtFind.Text.Trim()))
                    {
                        this.txtFind.Select();
                        this.txtFind.SelectAll();
                    }
                    else
                    {
                        //this.Find();
                    }
                }
            }
            else if (keyData.Equals(Keys.F4))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Undo();
                else
                    this.Edit();
                this.ResetControl();
            }
            else if (keyData.Equals(Keys.Control | Keys.D))
            {
                if (!(this.plAddMode || this.plEditMode))
                    this.Delete();
                this.ResetControl();
            }
            //else if (keyData.Equals(Keys.Control | Keys.P))
            //{
            //    if (!(this.plAddMode || this.plEditMode))
            //        //this.Preview();
            //}
            return base.ProcessDialogKey(keyData);
        }
        public void Gan_Click()
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }

        private void cmdFirst_Click(object sender, EventArgs e)
        {
            dsTonkho.MoveFirst();
        }

        private void cmdPrev_Click(object sender, EventArgs e)
        {
            dsTonkho.MovePrevious();
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            dsTonkho.MoveNext();
        }

        private void cmdLast_Click(object sender, EventArgs e)
        {
            dsTonkho.MoveLast();
        }

        private void grdList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Lỗi trong quá trình thực hiện" + ex.Message);

            }
        }

        private void grdList_Click(object sender, EventArgs e)
        {
            if (grdList.RowCount > 0)
            {
                Gan_Click();
                vitri = grdList.CurrentCell.RowIndex;

            }
        }
    }
}